package com.test.demo;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Random;

public class ProduceCSV {

    private static final Random random = new Random();

    // 车名列表
    private static final String[] CAR_NAMES = {
            "奔驰C级", "奔驰E级", "奔驰S级", "宝马3系", "宝马5系",
            "奥迪A4", "奥迪A6", "特斯拉Model 3", "特斯拉Model Y", "保时捷911"
    };

    // 颜色列表
    private static final String[] COLORS = {
            "白", "黑", "银", "深灰", "蓝", "红", "棕", "金", "绿", "紫"
    };

    // 仓库列表
    private static final String[] WAREHOUSES = {
            "北京仓库", "上海仓库", "广州仓库", "深圳仓库", "成都仓库",
            "杭州仓库", "武汉仓库", "西安仓库", "重庆仓库", "南京仓库"
    };

    // 状态列表
    private static final String[] STATUSES = {
            "已售出", "未售出"/*, "待交付", "在途运输", "已预订"*/
    };

    public static void main(String[] args) {
        // 生成数据并保存到CSV文件
        generateAndSaveCarCSV("所有车辆数据car_data.csv", 300);
        System.out.println("所有车辆数据car_data.csv");
    }

    public static void generateAndSaveCarCSV(String filename, int count) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
            // 写入CSV表头
            writer.write("ID,车名,颜色,价格,存储地点,时间,状态,创建人");
            writer.newLine();

            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

            for (int i = 0; i < count; i++) {
                int id = 19000 + i;
                String carName = CAR_NAMES[random.nextInt(CAR_NAMES.length)];
                String color = COLORS[random.nextInt(COLORS.length)];
                int price = generatePriceBasedOnModel(carName);
                String warehouse = WAREHOUSES[random.nextInt(WAREHOUSES.length)];
                LocalDate date = LocalDate.of(random.nextInt(WAREHOUSES.length) % 2 == 0 ? 2024 : 2025, 1 + random.nextInt(12), 1 + random.nextInt(28));
                String status = STATUSES[random.nextInt(STATUSES.length)];
                String creator = random.nextInt(WAREHOUSES.length) % 2 == 0 ? "admin" : "system";

                // 格式化CSV行(注意: 不包含空格)
                   /* String csvLine = String.format(
                            "%d,%s,%s,%d,%s,%s,%s",
                            id, carName, color, price, warehouse, date.format(formatter), status
                    );*/

                // 将字段用双引号括起来
                String csvLine = String.format(
                        "%d,\"%s\",\"%s\",%d,\"%s\",%s,\"%s\",\"%s\"",
                        id, carName, color, price, warehouse, date.format(formatter), status, creator
                );

                writer.write(csvLine);
                writer.newLine();
            }
        } catch (IOException e) {
            System.err.println("写入CSV文件时出错: " + e.getMessage());
        }
    }

    private static int generatePriceBasedOnModel(String carName) {
        if (carName.contains("C级") || carName.contains("3系") || carName.contains("A4")) {
            return 300000 + random.nextInt(200000); // 30-50万
        } else if (carName.contains("E级") || carName.contains("5系") || carName.contains("A6")) {
            return 500000 + random.nextInt(600000); // 50-110万
        } else if (carName.contains("S级") || carName.contains("7系") || carName.contains("A8")) {
            return 800000 + random.nextInt(1200000); // 80-200万
        } else if (carName.contains("911")) {
            return 1300000 + random.nextInt(2000000); // 130-330万
        } else {
            return 400000 + random.nextInt(800000); // 40-120万(其他车型)
        }
    }
}
